# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: panel_widget.py
  @time: 2022/7/4 15:07
  @desc:
"""

from PyQt5 import QtWidgets
from PyQt5 import sip
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QStandardItemModel, QStandardItem,QFont,QColor
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QPushButton, QVBoxLayout, QTableView, QFrame, QAbstractItemView, \
    QMessageBox,QLabel,QGraphicsDropShadowEffect


class PanelWidget(QWidget):
    def __init__(self, *args, **kwargs):
        super(PanelWidget, self).__init__(*args, **kwargs)
        self.setWindowFlags(Qt.FramelessWindowHint)
        self.__fl_style_sheet = "#Frame {background-color: #F9F8F3;border-radius:10px;border:3px solid #E8ECFF;}"
        self.setStyleSheet(self.__fl_style_sheet)
        self._setup_ui()

    def _setup_ui(self):
        self.vl_main = QVBoxLayout(self)
        self.vl_main.setContentsMargins(10, 5, 10, 5)
        self.frame = QFrame()
        self.frame.setObjectName("Frame")
        self.frame.setLineWidth(2)
        self.frame.setFrameStyle(QFrame.WinPanel | QFrame.Raised)
        self.vl_frame = QVBoxLayout(self.frame)
        self.lb_title = QLabel()
        self.lb_title.setObjectName("Title")
        self.lb_title.setFont(QFont("微软雅黑", 18))
        self.vl_frame.addWidget(self.lb_title, alignment=Qt.AlignTop)

        self.effect = QGraphicsDropShadowEffect(self)
        self.effect.setBlurRadius(20)
        self.effect.setOffset(0, 0)
        self.effect.setColor(QColor("#9FD4FD"))
        self.frame.setGraphicsEffect(self.effect)

        self.vl_main.addWidget(self.frame)
        self.set_title('Operator Info')
        self.set_title_font(QFont("微软雅黑", 10, 500, True))
        self.set_title_position(6)
        self.set_title_color((222, 150, 65))

    def set_title(self, title_name):
        try:
            self.lb_title.setText(title_name)
        except TypeError as e:
            print(e)
            print("Please pass in a title' name, and it must be str type. For example: 'Operator'")

    def set_widget(self, widget_obj):
        try:
            if len(self.frame.children()) == 3:
                self.vl_frame.removeItem(self.vl_frame.itemAt(1))
                self.frame.children()[2].deleteLater()
            wd_new = widget_obj
            self.vl_frame.addWidget(wd_new)
        except TypeError as e:
            print(e)
            print("Please pass in a QWidget object which you want to show in the panel.")

    def set_effect_color(self, q_color_obj):
        try:
            self.effect.setColor(q_color_obj)
        except TypeError as e:
            print(e)
            print("Please pass in a QColor object. For example: QColor(255, 22, 11).")

    def set_border_color(self, color):
        try:
            if type(color) == str:
                self.__fl_style_sheet += f"#Title {{color: {color};}}"
            elif type(color) == tuple:
                one, two, three = color
                new_style = f'#Frame {{border:3px solid rgb({str(one)}, {str(two)}, {str(three)});}}'
                self.__fl_style_sheet += new_style
            self.frame.setStyleSheet(self.__fl_style_sheet)
        except TypeError as e:
            print(e)
            print("Please pass in a tuple with RGB values or HEX color value. For example: (255, 255, 255) or #ffffff")

    def set_title_font(self, font_obj):
        try:
            self.lb_title.setFont(font_obj)
        except TypeError as e:
            print(e)
            print("Please pass in a QFont object. For example: QFont('宋体', 12, 500, True)")

    def set_title_position(self, left_value):
        try:
            self.lb_title.setStyleSheet(f"#Title {{padding-left: {left_value}px;}}")
        except TypeError as e:
            print(e)
            print("Please pass in a position value, and it must be int type. For example: 10")

    def set_title_color(self, color):
        try:
            if type(color) == str:
                self.__fl_style_sheet += f"#Title {{color: {color};}}"
            elif type(color) == tuple:
                one, two, three = color
                new_style = f'#Title {{color: rgb({str(one)}, {str(two)}, {str(three)});}}'
                self.__fl_style_sheet += new_style
            self.frame.setStyleSheet(self.__fl_style_sheet)
        except TypeError as e:
            print(e)
            print("Please pass in a tuple with RGB values or HEX color value. For example: (255, 255, 255) or #ffffff")

    def set_background_color(self, color):
        try:
            if type(color) == str:
                self.__fl_style_sheet += f"#Frame {{background-color: {color};}}"
            elif type(color) == tuple:
                one, two, three = color
                self.__fl_style_sheet += f"#Frame {{background-color: rgb({str(one)}, {str(two)}, {str(three)});}}"
            self.frame.setStyleSheet(self.__fl_style_sheet)
        except TypeError as e:
            print(e)
            print("Please pass in a tuple with RGB values or HEX color value. For example: (255, 255, 255) or #ffffff")

    def set_border_thickness(self, thickness):
        try:
            self.frame.setLineWidth(thickness)
        except TypeError as e:
            print(e)